package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.TblCourseMapper;
import com.jlkf.dao.TblFeedbackMapper;
import com.jlkf.dao.TblProblemMapper;
import com.jlkf.dao.TblSysUserMapper;
import com.jlkf.pojo.TblCourse;
import com.jlkf.pojo.TblFeedback;
import com.jlkf.pojo.TblProblem;
import com.jlkf.pojo.TblSysUser;
import com.jlkf.service.ProblemService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.PlatUtils;
import com.platform.server.util.RSA;
import com.platform.server.util.StringTools;

@Service
public class ProblemServiceImpl implements ProblemService {

	private static final String Long = null;
	@Autowired
	private TblProblemMapper tblProblemMapper;
	@Autowired
	private TblCourseMapper tblCourseMapper;
	@Autowired
	private TblFeedbackMapper tblfeedbackMapper;
	@Autowired
	private TblSysUserMapper tblSysUserMapper;
	
	 /**
     * pc端技术提问列表
     */
	@Override
	public ReturnData selectProblemList(Integer userid,Integer courseid) {
		if(courseid == null){
			return new ReturnData(12,"参数有误");
		}
		List<Map<String,Object>> lists = tblProblemMapper.selectProblemList(courseid);
		for(int i = 0;i < lists.size();i++){
			//查看用户会员等级
			Long uid = (Long)lists.get(i).get("userid");
			String vipName = tblProblemMapper.selectUserVip(uid.intValue());
			lists.get(i).put("vipName", vipName);
		}
		return new ReturnData(lists,200,"成功");
	}

	/**
     * pc端提交技术问答
     */
	@Override
	public ReturnData insertSelective(TblProblem record) throws Exception {
		if(record.getgUserId() == null || record .getgCourseId() == null || record.getgPutContent() == null){
			return new ReturnData(12,"参数有误");
		}
		//查看敏感词汇
		String[] words = tblProblemMapper.selectSensitives();
		for(int i = 0;i < words.length;i++){
			String decodedData = RSA.privateDecrypt(words[i], RSA.getPrivateKey(RSA.privateKey));
			if(record.getgPutContent().contains(decodedData)){
				return new ReturnData(15,"您所编辑的内容含有敏感词汇，请重新编辑！");
			}
		}
		record.setgCreatetime(new Date());
		int flag = tblProblemMapper.insertSelective(record);
		if(flag > 0){
			return new ReturnData(200,"成功");
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 我的问答
	 */
	@Override
	public ReturnData getMyProblem(Integer userId,Integer type,Integer pageNumber,Integer pageSize) {
		// TODO Auto-generated method stub
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=1000;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("type", type);
		PageHelper.startPage(pageNumber, pageSize);
		List<TblProblem> list = tblProblemMapper.getProblemList(map);
		for(TblProblem problem:list){
			TblCourse course = tblCourseMapper.selectByPrimaryKey(problem.getgCourseId());
			if(course!=null){
				problem.setCourseName(course.getgCourseName());
				problem.setPicture(course.getgCourseImg());
			}
			if(problem.getgReplyId()!=null){
				TblSysUser sysUser = tblSysUserMapper.selectByPrimaryKey(problem.getgReplyId().intValue());
				if(sysUser!=null){
					problem.setRealyName(sysUser.getgName());
				}
			}
		}
		PageInfo<TblProblem> pageInfo = new PageInfo<>(list);
		return new ReturnData(list, pageInfo.getPages(), (int)pageInfo.getTotal(), 200, "我的问答");
	}

	@Override
	public ReturnData deleteProblem(Integer id) {
		// TODO Auto-generated method stub
		int num = tblProblemMapper.deleteByPrimaryKey((long)id);
		if(num>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}

	/**
	 * 意见反馈
	 */
	@Override
	public ReturnData insertFeedBack(String json) {
		// TODO Auto-generated method stub
		try{
			TblFeedback feedback = StringTools.json2Obj(json, TblFeedback.class);
			if(feedback.getgUserid()==null){
				return new ReturnData(500,"用户信息不能为空");
			}
			if(StringTools.isEmpty(feedback.getgContent())){
				return new ReturnData(500,"内容不能为空");
			}
			feedback.setgCreateTime(new Date());
			int rel = tblfeedbackMapper.insertSelective(feedback);
			if(rel>0){
				return new ReturnData(200,"发送成功");
			}else{
				return new ReturnData(200,"发送失败");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}

}
